/**
 *  字典
 *      let dict = new Dictionary<string,string>();
 *      dict.set('mytest','HAHA');
 *      dict.set('utest','BOBO');
 *      dict.delete('mytest'); //return item's [mytest] value : 'HAHA'
 *      dict.getWithComponent('mytest');//null
 *      dict.getWithComponent('');//null
 *      dict.getWithComponent('utest');//"BOBO"
 */
export class Dictionary<TypeKeyT, TypeValueT> {
    private _typeKeyList: TypeKeyT[] = new Array<TypeKeyT>();
    private _typeValueList: TypeValueT[] = new Array<TypeValueT>();

    public set(key: TypeKeyT, value: TypeValueT): void {
        let index = this._typeKeyList.indexOf(key);
        if (index == -1) {
            this._typeKeyList.push(key);
            index = this._typeKeyList.indexOf(key);
        }
        this._typeValueList[index] = value;
    }

    public get(key: TypeKeyT): TypeValueT {
        let index = this._typeKeyList.indexOf(key);
        if (index != -1) {
            return this._typeValueList[index];
        }
        return null;
    }

    public getKey(value: TypeValueT): TypeKeyT {
        let index = this._typeValueList.indexOf(value);
        if (index != -1) {
            return this._typeKeyList[index];
        }
        return null;
    }

    public delete(key: TypeKeyT): TypeValueT {
        let index = this._typeKeyList.indexOf(key);
        if (index != -1) {
            this._typeKeyList.splice(index, 1);
            let deleteList = this._typeValueList.splice(index, 1);
            return deleteList[0];
        }
        return null;
    }

    public deleteValue(value: TypeValueT): TypeKeyT {
        let index = this._typeValueList.indexOf(value);
        if (index != -1) {
            this._typeValueList.splice(index, 1);
            let deleteList = this._typeKeyList.splice(index, 1);
            return deleteList[0];
        }
        return null;
    }


    public clear(): void {
        for (let k = this._typeKeyList.length - 1; k >= 0; --k) {
            this.delete(this._typeKeyList[k]);
        }
    }

    public containsKey(key: TypeKeyT): boolean {
        if (this._typeKeyList.indexOf(key) >= 0) {
            return true;
        }
        return false;
    }

    public containsValue(value: TypeValueT): boolean {
        if (this._typeValueList.indexOf(value) >= 0) {
            return true;
        }
        return false;
    }


    public getKeyList(): any[] {
        return this._typeKeyList;
    }

    public getValueList(): any[] {

        return this._typeValueList;
    }

    get length(){
        return this._typeKeyList.length;
    }
}